
package com.example.control;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class InitActivity extends Activity {

    private EditText ip, port, username;

    private Button nextButton;

    private String getip, getport, getusername;

    private InetSocketAddress isa = null;

    private ProgressDialog progressDialog;

    private Handler mLoginHandler;

	private SharedPreferences mLoginInfoPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.config);

        mLoginInfoPreferences = getSharedPreferences("logininfo", MODE_PRIVATE);
        
        
        ip = (EditText) findViewById(R.id.ip);
        port = (EditText) findViewById(R.id.port);
        username = (EditText) findViewById(R.id.username);
        ip.setText(mLoginInfoPreferences.getString("ip", ""));
        port.setText(mLoginInfoPreferences.getString("port", ""));
        username.setText(mLoginInfoPreferences.getString("username", ""));
        nextButton = (Button) findViewById(R.id.next);
        nextButton.setOnClickListener(new nextButtonListenner());
        mLoginHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 0:
                        Toast.makeText(InitActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(InitActivity.this, ClientMainActivity.class);
                        Editor edit = mLoginInfoPreferences.edit();
                        edit.putString("ip", ip.getEditableText().toString().trim());
                        edit.putString("port", port.getEditableText().toString().trim());
                        edit.putString("username", username.getEditableText().toString().trim());
                        edit.commit();
                        startActivity(intent);
                        InitActivity.this.finish();
                        break;
                    case 1:
                        Toast.makeText(InitActivity.this, "连接失败,端口或IP不可用", Toast.LENGTH_SHORT)
                                .show();
                        progressDialog.dismiss();
                        break;
                    case 2:
                        Toast.makeText(InitActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                        break;
                    case 3:
                        Toast.makeText(InitActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                        break;

                    default:
                        progressDialog.dismiss();
                        break;
                }
            }

        };
    }

    @Override
    protected void onDestroy() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        super.onDestroy();
    }

    class nextButtonListenner implements OnClickListener {

        @Override
        public void onClick(View v) {
            getip = ip.getText().toString().trim();
            getport = port.getText().toString().trim();
            getusername = username.getText().toString().trim();
            if (getip == "" || getip == null || getip.equals("")) {
                // Toast.makeText(InitActivity.this, "请输入IP",
                // Toast.LENGTH_SHORT).show();
                // ip.setFocusable(true);
                getip = "10.15.73.47";
            }
            // else
            if (getport == "" || getport == null || getport.equals("")) {
                // Toast.makeText(InitActivity.this, "请输入端口",
                // Toast.LENGTH_SHORT).show();
                // port.setFocusable(true);
                getport = "6666";
            }
            // else
            if (getusername == "" || getusername == null || getusername.equals("")) {
                // Toast.makeText(InitActivity.this, "请输入用户名",
                // Toast.LENGTH_SHORT).show();
                // username.setFocusable(true);
                getusername = "user";
            }
            progressDialog = ProgressDialog.show(InitActivity.this, "", "请稍后...", true, false);
            new Thread() {
                @Override
                public void run() {
                    try {
                        Socket s = new Socket();
                        isa = new InetSocketAddress(getip, Integer.parseInt(getport));
                        s.connect(isa, 5000);
                        if (s.isConnected()) {
                            ConectParams.sDataOut = new DataOutputStream(s.getOutputStream());
                            ConectParams.sDataIn = new DataInputStream(s.getInputStream());
                        }
                        ConectParams.LOCAL_ADDRESS = s.getLocalSocketAddress();
                        ConectParams.IP = getip;
                        ConectParams.PORT = getport;
                        ConectParams.USERNAME = getusername;
                        mLoginHandler.sendEmptyMessage(0);
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                        mLoginHandler.sendEmptyMessage(1);
                    } catch (SocketTimeoutException e) {
                        System.out.println("连接超时");
                        mLoginHandler.sendEmptyMessage(2);
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                        mLoginHandler.sendEmptyMessage(3);
                    }

                }
            }.start();
        }
    }

}
